import { Toast } from 'antd-mobile';
const IP = "http://124.71.234.61:21021";
//const IP = "http://172.16.2.101:21021";
const request = (url, config) => {
    return fetch(IP + url, config).then(function (response) { return response.json() })
        .then((data) => {
            return data;
        })
        .catch((error) => {
            console.log(error);
            Toast.fail('fetch data failed', error);
        });
};

// GET请求
export const get = (url) => {
    return request(url, {
        method: 'GET',
        headers: {
            'content-type': 'application/json',
            'Abp.TenantId': 1,
            'Abp-TenantId': 1,
            'Authorization': 'Bear ' + localStorage.getItem('accessToken')
        },
    });
};

// POST请求
export const post = (url, data) => {
    return request(url, {
        body: JSON.stringify(data),
        headers: {
            'content-type': 'application/json',
            'Abp.TenantId': 1,
            'Abp-TenantId': 1,
            'Authorization': 'Bear ' + localStorage.getItem('accessToken')
        },
        method: 'POST'
    });
};

// POST请求
export const put = (url, data) => {
    return request(url, {
        body: JSON.stringify(data),
        headers: {
            'content-type': 'application/json',
            'Abp.TenantId': 1,
            'Abp-TenantId': 1,
            'Authorization': 'Bear ' + localStorage.getItem('accessToken')
        },
        method: 'PUT'
    });
};

export const imgServer = IP + "/Home/GetImageAsync?sysRowId=";
export const uploadPic = async (files, objectId, objectName, objectField) => {
    let formData = new FormData();
    files.forEach(element => {
        formData.append("files", element.file);
    });
    formData.append("objectId", objectId);
    formData.append("objectName", objectName);
    formData.append("objectField", objectField);

    var resp = {};
    await fetch(IP + "/Home/UploadFilesAsync", {
        method: 'POST',
        headers: {
            'Authorization': 'Bearer ' + localStorage.getItem("accessToken"),
        },
        body: formData,
    }).then((response) => response.json())
        .then((responseData) => {
            resp = responseData;
        }).catch((err) => {
            Toast.fail("图片上传失败");
        });
    if (resp && resp.success) {
        return resp.result;
    }
    else {
        return "";
    }
};
export const getAge = (str) => {
    var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})/);
    if (r == null) return false;

    var d = new Date(r[1], r[3] - 1, r[4]);
    var returnStr = "输入的日期格式错误！";

    if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {

        var date = new Date();
        var yearNow = date.getFullYear();
        var monthNow = date.getMonth() + 1;
        var dayNow = date.getDate();

        var largeMonths = [1, 3, 5, 7, 8, 10, 12], //大月， 用于计算天，只在年月都为零时，天数有效
            lastMonth = monthNow - 1 > 0 ? monthNow - 1 : 12,  // 上一个月的月份
            isLeapYear = false, // 是否是闰年
            daysOFMonth = 0;    // 当前日期的上一个月多少天

        if ((yearNow % 4 === 0 && yearNow % 100 !== 0) || yearNow % 400 === 0) {  // 是否闰年， 用于计算天，只在年月都为零时，天数有效
            isLeapYear = true;
        }

        if (largeMonths.indexOf(lastMonth) > -1) {
            daysOFMonth = 31;
        } else if (lastMonth === 2) {
            if (isLeapYear) {
                daysOFMonth = 29;
            } else {
                daysOFMonth = 28;
            }
        } else {
            daysOFMonth = 30;
        }

        var Y = yearNow - parseInt(r[1]);
        var M = monthNow - parseInt(r[3]);
        var D = dayNow - parseInt(r[4]);
        if (D < 0) {
            D = D + daysOFMonth; //借一个月
            M--;
        }
        if (M < 0) {  // 借一年 12个月
            Y--;
            M = M + 12; //
        }

        if (Y < 0) {
            returnStr = "出生日期有误！";

        } else if (Y === 0) {
            if (M === 0) {
                returnStr = D + "天";
            } else {
                returnStr = M + "个月";
            }
        } else {
            if (M === 0) {
                returnStr = Y + "岁";
            } else {
                returnStr = Y + "岁" + M + "个月";
            }
        }

    }

    return returnStr;
}
export const getBMI = (weight, height) => {
    return (weight / Math.pow(height / 100, 2)).toFixed(2)
}
export const noteBMI = (bmi, ageMonth, dataBMI, sex = 1) => {
    var data = dataBMI.filter(o => o.age == ageMonth && o.sex == sex)[0]
    if (data) {
        if (bmi < data.sD1 && bmi >= data.sdB2) {
            return '正常'
        }
        else if (bmi <= data.sD2 && bmi > data.sD1) {
            return '超重'
        }
        else if (bmi > data.sD3) {
            return '肥胖'
        }
        else if (bmi < data.sdB2 && bmi >= data.sdB3) {
            return '消瘦'
        }
        else if (bmi < data.sdB3) {
            return '严重消瘦'
        }
    } else {
        if (bmi >= 17 && bmi <= 24)
            return '正常'
        else if (bmi > 24)
            return '肥胖'
        else if (bmi < 17)
            return '消瘦'
        else
            return ''
    }
}
const BMIBZ = [
    {
        sex: 1,
        age: 0,
        SDb3: 10.2,
        SDb2: 11.1,
        SDb1: 12.2,
        SD: 13.4,
        SD1: 14.8,
        SD2: 16.3,
        SD3: 18.1,
    },
    {
        sex: 1,
        age: 1,
        SDb3: 11.3,
        SDb2: 12.4,
        SDb1: 13.6,
        SD: 14.9,
        SD1: 16.3,
        SD2: 17.8,
        SD3: 19.4,
    },
]
export const getGweek = (day1, day2) => {
    var startDate = Date.parse(day1);
    var endDate = Date.parse(day2);
    var days = (startDate - endDate) / (1 * 24 * 60 * 60 * 1000);
    return 40 + parseInt(days / 7);
}
export const mingzu = ("汉族、满族、蒙古族、回族、藏族、维吾尔族、苗族、彝族、壮族、布依族、侗族、瑶族、白族、土家族、哈尼族、哈萨克族、傣族、黎族、傈僳族、佤族、畲族、高山族、拉祜族、水族、东乡族、纳西族、景颇族、柯尔克孜族、土族、达斡尔族、仫佬族、羌族、布朗族、撒拉族、毛南族、仡佬族、锡伯族、阿昌族、普米族、朝鲜族、塔吉克族、怒族、乌孜别克族、俄罗斯族、鄂温克族、德昂族、保安族、裕固族、京族、塔塔尔族、独龙族、鄂伦春族、赫哲族、门巴族、珞巴族、基诺族").split('、');